package com.example.farmer.Database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.farmer.Entity.CirclePost;

import java.util.ArrayList;
import java.util.List;


public class CirclePostDatabase extends SQLiteOpenHelper {
//    private int id; // 唯一标识符
//    private String content; // 帖子内容
//    private String imageUrl; // 帖子图片的路径或URL
    private static CirclePostDatabase instance;
        private static final String DATABASE_NAME = "farmer_circle_post.db";
        private static final int DATABASE_VERSION = 1;
        private static final String TABLE_NAME = "circle_posts";
        private static final String COLUMN_ID = "id";
        private static final String COLUMN_CONTENT = "content";
        private static final String COLUMN_IMAGE_URL = "image_url";
    private static final String COLUMN_NUMBER = "userNumber";


        private static final String CREATE_TABLE = "CREATE TABLE "
                + TABLE_NAME + "("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_CONTENT + " TEXT,"
                + COLUMN_IMAGE_URL + " TEXT,"
                + COLUMN_NUMBER + " TEXT"+" )";


        public CirclePostDatabase(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

    public static synchronized CirclePostDatabase getInstance(Context context) {
        if (instance == null) {
            instance = new CirclePostDatabase(context);
        }
        return instance;
    }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }

        // 插入帖子的方法
        public long insertPost(String content, String imageUrl) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(COLUMN_CONTENT, content);
            values.put(COLUMN_IMAGE_URL,imageUrl);

            long postId = db.insert(TABLE_NAME, null, values);
            return postId;        }

        // 查询所有帖子的方法
        @SuppressLint("Range")
        public List<CirclePost> getAllPosts() {
            List<CirclePost> circlePostList = new ArrayList<>();
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

            if (cursor.moveToFirst()) {
                do {
                    CirclePost circlePost = new CirclePost();
                    circlePost.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
                    circlePost.setContent(cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)));
                    // 如果有图片URL，也添加到Post对象
                    circlePost.setImageUrl(cursor.getString(cursor.getColumnIndex(COLUMN_IMAGE_URL)));
                    circlePost.setUserNumber(cursor.getString(cursor.getColumnIndex(COLUMN_NUMBER)));
                    circlePostList.add(circlePost);

                } while (cursor.moveToNext());
            }
            cursor.close();
            db.close();
            return circlePostList;
        }

        // 更新帖子的方法
        public int updatePost(long id, String content) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(COLUMN_CONTENT, content);
            return db.update(TABLE_NAME, values, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
        }

        // 删除帖子的方法
        public void deletePost(long id) {
            SQLiteDatabase db = this.getWritableDatabase();
            db.delete(TABLE_NAME, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
        }

    public List<CirclePost> getPostsByUser(String userNumber) {
        List<CirclePost> circlePostList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, COLUMN_NUMBER + "=?",
                new String[]{userNumber}, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                CirclePost circlePost = new CirclePost();
                circlePost.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
                circlePost.setContent(cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)));
                circlePost.setImageUrl(cursor.getString(cursor.getColumnIndex(COLUMN_IMAGE_URL)));
                circlePostList.add(circlePost);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return circlePostList;
    }
//关注

    // 添加关注
    public void addChoose(String userNumber) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NUMBER, userNumber);
        db.insert(TABLE_NAME, null, values);
    }

    // 检查是否已关注
    public boolean isChooseNumber(String userNumber) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = {COLUMN_NUMBER};
        String selection = COLUMN_NUMBER + " = ?";
        String[] selectionArgs = {userNumber};
        Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
        try {
            return cursor.getCount() > 0;
        } finally {
            cursor.close();
            db.close();
        }
    }

    public String getColumnContent(String userNumber) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = { "content and image_url" };
        String selection = "userNumber = ?";
        String[] selectionArgs = { userNumber };
        String content = null;
        Cursor cursor = null;
        try {
            cursor = db.query("circle_posts", columns, selection, selectionArgs, null, null, null);
            if (cursor.moveToFirst()) {
                content = cursor.getString(0); // 索引0对应content
            }
        } catch (Exception e) {
            // 异常处理
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return content;
    }
}
